package com.running.mms.smil;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class SmilFileWriter {
	private Element smil = new Element("smil");
	private Element head = new Element("head");
	private Element body = new Element("body");
	private Element layout = new Element("layout");
	private Element rootLayout = new Element("root-layout");
	private Element topRegion = new Element("region");
	private Element bottomRegion = new Element("region");
	
	private Element newHead(){
		head.addContent(newLayout());
		return head;
	}
	
	public void setTitle(String title){
		head.addContent(newMeta("title",title));
	}
	
	private Element newBody(){
		return body;
	}
	
	private Element newLayout(){
		layout.addContent(newRootLayout());
		layout.addContent(newTopRegion());
		layout.addContent(newBottomRegion());
		return layout;
	}
	
	private Element newRootLayout(){
		rootLayout.setAttribute("width","200");
		rootLayout.setAttribute("height","300");
		return rootLayout;
	}
	
	private Element newTopRegion(){
		topRegion.setAttribute("id", "top0");
		topRegion.setAttribute("width", "200");
		topRegion.setAttribute("height", "150");
		topRegion.setAttribute("left", "0");
		topRegion.setAttribute("top", "0");
		return topRegion;
	}
	
	private Element newBottomRegion(){
		bottomRegion.setAttribute("id", "bottom0");
		bottomRegion.setAttribute("width", "200");
		bottomRegion.setAttribute("height", "150");
		bottomRegion.setAttribute("left", "0");
		bottomRegion.setAttribute("top", "150");
		return bottomRegion;
	}
	
	private Element newMeta(String name,String content){
		Element meta = new Element("meta");
		meta.setAttribute("name", name);
		meta.setAttribute("content", content);
		return meta;
	}
	
	public Element newPar(){
		return newPar("5000");
	}
	
	public Element newPar(String dur){
		Element par = new Element("par");
		par.setAttribute("dur",dur+"ms");
		return par;
	}
	
	public void setPar(Element par){
		body.addContent(par);
	}
	
	public Element newImg(String path){
		Element img = new Element("img");
		img.setAttribute("src",path);
		img.setAttribute("region","top0");
		return img;
	}
	
	public Element newAudio(String path){
		Element audio = new Element("audio");
		audio.setAttribute("src",path);
		audio.setAttribute("region","top0");
		return audio;
	}
	
	public Element newText(String path){
		Element text = new Element("text");
		text.setAttribute("src",path);
		text.setAttribute("region","bottom0");
		return text;
	}
	
	//返回的是文件内容
	public String build(){
		smil.addContent(newHead());
		smil.addContent(newBody());
        Document document = new Document(smil);  
        XMLOutputter out = new  XMLOutputter();
        String content = "";
        try {
        	content = out.outputString(document);
        } catch (Exception ex) {
        	ex.printStackTrace();
        }
        return content;
	}
}
